home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / PASCTXT.ZIP / CHAP10.TXT < prev    next >
Text File  |  1988-01-15  |  14KB  |  323 lines

  1.  
  2.                     CHAPTER 10 - Standard Input/Output
  3.  
  4.  
  5.  
  6.              During  the course of this tutorial we have been  using
  7.         the Write and Writeln procedures to display data, and it  is
  8.         now time to discuss them fully.  Actually there is little to
  9.         be said that has not already been said, but in order to  get
  10.         all of the data in one place they will be redefined here.
  11.  
  12.              As  mentioned  earlier,  Write  and  Writeln  are   not
  13.         actually  reserved words but are procedure calls.  They  are
  14.         therefore merely identifiers that could be changed but there
  15.         should never be a reason to do so.  Lets get on to our first
  16.         example program WRITELNX which has lots of output.
  17.  
  18.                           MANY OUTPUT STATEMENTS
  19.  
  20.              Pascal has two output statements that are only slightly
  21.         different  in  the  way they work.   The  Writeln  statement
  22.         outputs  all of the data specified within it,  then  returns
  23.         the  cursor  to the beginning of the next line.   The  Write
  24.         statement outputs all of the data specified within it,  then
  25.         leaves  the  cursor at the next character  where  additional
  26.         data  can be output.  The Write statement can  therefore  be
  27.         used  to  output a line in bits and pieces  if  desired  for
  28.         programming convenience.  The first example program for this
  29.         chapter,  WRITELNX,  has  many output  statements  for  your
  30.         observation.   All outputs are repeated so you  can  observe
  31.         where the present field ends and the next starts.
  32.  
  33.              Observe the integer output statements beginning in line
  34.         13.   The  first simply directs the system to  output  Index
  35.         twice,  and it outputs the value with no separating  blanks.
  36.         The second statement says to output Index twice also, but it
  37.         instructs  the  system  to put each output  in  a  field  15
  38.         characters wide with the data right justified in the  field.
  39.         This  makes the output look much better.   This  illustrates
  40.         that  you have complete control over the appearance of  your
  41.         output data.
  42.  
  43.              The  real output statements are similar to the  integer
  44.         except that when the data is put into a field 15  characters
  45.         wide, it is still displayed in scientific format.  Adding  a
  46.         second field descriptor tells the system how many digits you
  47.         want displayed after the decimal point.  Lines 21 through 23
  48.         illustrate the second field and its use.
  49.  
  50.              The  boolean, char, and string examples should be  self
  51.         explanatory.   Notice that when the string is  output,  even
  52.         though  the  string  has been defined as  a  maximum  of  10
  53.         characters,  it  has  been  assigned  a  string  of  only  8
  54.         characters,  so only 8 characters are output.
  55.  
  56.  
  57.  
  58.                                   Page 58
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                     CHAPTER 10 - Standard Input/Output
  69.  
  70.  
  71.              Compile and run this program and observe the results.
  72.  
  73.              If  you are using TURBO Pascal version 4.0,  the  added
  74.         data  types  described  in chapter 3 of  this  tutorial  are
  75.         output  in  the  same manner as  those  illustrated  in  the
  76.         program WRITELNX.
  77.  
  78.                    NOW FOR SOME INPUT FROM THE KEYBOARD
  79.  
  80.              The  example file READINT will illustrate reading  some
  81.         integer data from the keyboard.  A message is output in line
  82.         8  with  an  interesting fact that should  be  pointed  out.
  83.         Anyplace where Pascal uses a string variable or constant, it
  84.         uses  the apostrophe for a delimiter.   Therefore,  anyplace
  85.         where  an  apostrophe is used in a string, it will  end  the
  86.         string.   Two  apostrophes in a row will be construed  as  a
  87.         single  apostrophe within the string and will not  terminate
  88.         the  string.   The  term  'Read'  within  the  string   will
  89.         therefore be displayed as shown earlier in this sentence.
  90.  
  91.              The variable Index is used to loop five times through a
  92.         sequence  of statements with one Read statement in it.   The
  93.         three  integer  values  are  read in  and  stored  in  their
  94.         respective  variables with the one statement.  If less  than
  95.         three are entered at the keyboard, only as many as are  read
  96.         in  will be defined, the rest will be unchanged.   Following
  97.         completion  of  the first loop, there is a  second  loop  in
  98.         lines 19 through 25 that will be executed 5 times with  only
  99.         one  minor  change, the Read statement is  replaced  by  the
  100.         Readln  statement.  At this point it would be best run  this
  101.         program trying several variations with input data.
  102.  
  103.              When  you run READINT, it will request three  integers.
  104.         Reply with three small integers of your choice with as  many
  105.         blank  spaces  between  each as you desire,  followed  by  a
  106.         carriage  return.  The system will echo your  three  numbers
  107.         back  out,  and request three more.  Respond with  only  one
  108.         number  this time, different from each of the  first  three,
  109.         and  a  carriage  return.   You will  get  your  new  number
  110.         followed by your previous second and third number indicating
  111.         that  you did not re-enter the last two  integer  variables.
  112.         Enter  three  more numbers, this time including  a  negative
  113.         number and observe the echo once again.
  114.  
  115.              Continue entering numbers until the system outputs  the
  116.         message indicating that it will now be using the Readln  for
  117.         reading data.  At this point enter the same numbers that you
  118.         did in the previous section and notice the difference, which
  119.         is  only  very slight.  Each time you hit the enter  key  to
  120.         cause  the computer to process the data you have just  given
  121.         it, it will echo the carriage return to the display, and the
  122.  
  123.  
  124.                                   Page 59
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                     CHAPTER 10 - Standard Input/Output
  135.  
  136.  
  137.         "Thank  you" message will be on a new line.   When  entering
  138.         data  from  the keyboard, the only difference  in  Read  and
  139.         Readln  is whether or not the carriage return is  echoed  to
  140.         the display following the data read operation.
  141.  
  142.              It should not be a surprise to you that after you enter
  143.         the  data, the data is stored within the program and can  be
  144.         used anywhere that integer data is legal for use.  Thus, you
  145.         could read in an integer, and use the integer to control the
  146.         number of times through a loop, as a case selector, etc.
  147.  
  148.                         TIME TO CRASH THE COMPUTER
  149.  
  150.              Crashing the computer will not hurt a thing.  Rerun the
  151.         above  program and instead of entering integer  data,  enter
  152.         some  real data with decimal points, or even some  character
  153.         data.   The  computer should display some  kind  of  message
  154.         indicating that you have caused an I/O error (Input/Output),
  155.         and TURBO Pascal will abort operation (that simply means  to
  156.         stop  the  program  and  return  control  to  the  operating
  157.         system).   No harm has been done, simply start it  again  to
  158.         enter more numbers or errors.
  159.  
  160.                            READING REAL NUMBERS
  161.  
  162.              The  example  program READREAL will illustrate  how  to
  163.         read  real  numbers  into the computer.   It  will  read  an
  164.         integer  and three real numbers each time through the  loop.
  165.         It  is perfectly fine to give the system a number without  a
  166.         decimal  point for a real number.  The computer will  simply
  167.         read  it  as a decimal number with zeros after  the  decimal
  168.         point  and consider it as a real number internally.  As  you
  169.         found  out in the last example program, however, it  is  not
  170.         permissible  to include a decimal point in the data  if  the
  171.         computer  is looking for an integer variable.  Include  some
  172.         character  data  for a real number and crash the  system  in
  173.         this program too.
  174.  
  175.                           READING CHARACTER DATA
  176.  
  177.              The  next example program, READCHAR, will read  in  one
  178.         character each time through the loop and display it for you.
  179.         Try  entering more than one character and you will see  that
  180.         the  extra  characters will simply be ignored.   It  is  not
  181.         possible  to  crash this program because any  character  you
  182.         enter will be valid.
  183.  
  184.              The  next example, READARRY, will read in a  string  of
  185.         characters  and display them for you if you are using  TURBO
  186.         Pascal 3.0.  TURBO Pascal 4.0 does not allow reading into an
  187.         array but does allow reading into the individual elements of
  188.  
  189.  
  190.                                   Page 60
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                     CHAPTER 10 - Standard Input/Output
  201.  
  202.  
  203.         the array one element at a time.  This program does not work
  204.         with TURBO Pascal 4.0 so you should go directly to the  next
  205.         program, READSTRG, if you are using that version.
  206.  
  207.              Continuing  our  discussion  of  READARRY,  up  to   10
  208.         characters  will be read, and if less than 10 are read,  the
  209.         rest will be blank filled.  Try entering 10 characters, then
  210.         4,  to  see that the residual 6 characters are  blanked  out
  211.         before  storing and printing.  Since the array is  fixed  at
  212.         ten  characters,  ten  characters are  always  printed  out,
  213.         including trailing blanks.
  214.  
  215.              Finally  READSTRG will also read up to  10  characters,
  216.         but  since  a string is a dynamic length variable,  it  will
  217.         only print out the characters you input each time, up to the
  218.         maximum  of 10 as defined in the var declaration.   It  will
  219.         display  trailing blanks if you type them in because  blanks
  220.         are valid characters.
  221.  
  222.                          BULLET PROOF PROGRAMMING
  223.  
  224.              It can be frustrating to be running a program and  have
  225.         it  declare  an  I/O error and  terminate  operation  simply
  226.         because  you  have  entered  an  incorrect  character.   The
  227.         integer and real data inputs defined earlier in this chapter
  228.         are   fine  for  quick  little  programs  to   do   specific
  229.         calculations,  but if you are writing a  large  applications
  230.         program  it is better to use another technique.   Since  the
  231.         character  and string inputs cannot abort operation  of  the
  232.         program,  it is best to use them to input the variable  data
  233.         and  check  the  data  internally  under  your  own  program
  234.         control.  An error message can then be given to the operator
  235.         and  another opportunity granted to input the correct  data.
  236.         All  well  written  large  application  programs  use   this
  237.         technique.
  238.  
  239.                  HOW DO I PRINT SOMETHING ON THE PRINTER
  240.  
  241.              With  all of the Pascal knowledge you now have,  it  is
  242.         the simplest thing in the world to get data to the  printer.
  243.         The  example file PRINTOUT will show you graphically how  to
  244.         do it.  Every Write or Writeln statement is required to have
  245.         a  device  identifier prior to the first output  field.   If
  246.         there is none, it is automatically defaulted to the standard
  247.         output device, the display monitor.  The example program has
  248.         a  few  outputs  to the monitor in lines 9 and 10  with  the
  249.         device  identifier included, namely "Output".  This is  only
  250.         done  to show you the general form of the Write  statements.
  251.         There  are  also many statements in this  program  with  the
  252.         display identifier "Lst", which is the standard name for the
  253.         list device or the printer.
  254.  
  255.  
  256.                                   Page 61
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                     CHAPTER 10 - Standard Input/Output
  267.  
  268.  
  269.  
  270.              Compile  and run this program with your printer  turned
  271.         on  for some printer output.  If you are using TURBO  Pascal
  272.         3.0,  you will have to comment out line 4 since it will  not
  273.         be understood by your compiler.  It is required with version
  274.         4.0 to tell the system where to find the output device  name
  275.         Lst.
  276.  
  277.              Just  to supply you with a bit more information,  every
  278.         Read and Readln statement is also required to have a  device
  279.         identifier  prior  to  the first input field.   As  you  may
  280.         suspect, it is also defaulted to Input if none is specified,
  281.         and the standard input device is the keyboard.
  282.  
  283.                            PROGRAMMING EXERCISE
  284.  
  285.         1.  Write a program containing a loop to read in a character
  286.             string  up to 60 characters long,  then print the string
  287.             on your printer. When you run the program, you will have
  288.             the  simplest word processing program in the  world.  Be
  289.             sure  to  include a test to end the loop,  such as  when
  290.             "END" is typed in.
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.                                   Page 62
  323.